package com.lkm.market2_0.m_imple.app_main;

import com.lkm.frame.module.ConditionConfige;
import com.lkm.frame.module.IModule;
import com.lkm.frame.module.ModuleBoot;
import com.lkm.frame.module.ModuleDelegate;
import com.lkm.frame.module.ModuleManager;
import com.lkm.frame.task2.InterceptorAdapter;
import com.lkm.frame.task2.Task;
import com.lkm.market2_0.AppDataCache;
import com.lkm.market2_0.entity.OrganizationInfo;
import com.lkm.market2_0.entity.ResponEntity;
import com.lkm.market2_0.m.app_main.AppMainModule;
import com.lkm.market2_0.m.steelyarddata.SteelyardDataModule;
import com.lkm.market2_0.m.steelyardserver.SteelyardServerModule;
import com.lkm.market2_0.m.steelyardstatus.SteelyardStatusManagerModule;

import java.util.Map;




public class AppMainModuleDelegate implements ModuleDelegate{

    ModuleManager mModuleManager;
    @Override
    public void init(ModuleManager moduleManager) {
        mModuleManager=moduleManager;
    }

    @Override
    public void dependency(DependencyConfig dc) {
        
        dc.addDependency(SteelyardServerModule.class);
        
        dc.addDependency(SteelyardStatusManagerModule.class);
        
        dc.addDependency(SteelyardDataModule.class);



        final ModuleBoot loadingModuleBoot= dc.getModuleBoot();
        

        LoadAppInitForSTask loadAppInitForSTask= LoadAppInitForSTask.load(AppDataCache.getInstance().getPushKey(),true);
        loadAppInitForSTask.addInterceptor(new InterceptorAdapter<String, ResponEntity<Map<Integer ,OrganizationInfo>>>(){
            @Override
            public boolean onInterceptCancel(Task<String, ?, ResponEntity<Map<Integer, OrganizationInfo>>> task) {
                loadingModuleBoot.errorForDependModuleFail();
                return super.onInterceptCancel(task);

            }


            @Override
            public boolean onInterceptFinish(Task<String, ?, ResponEntity<Map<Integer, OrganizationInfo>>> task,ResponEntity<Map<Integer ,OrganizationInfo>> responEntity) {
                if(!responEntity.isSuccess())
                loadingModuleBoot.errorForDependModuleFail();
                return super.onInterceptFinish(task, responEntity);
            }
        });

        
        loadingModuleBoot.dependency(loadAppInitForSTask);
    }

    @Override
    public void configure(ConditionConfige cc) {

    }

    @Override
    public IModule execute(Object o) {
        SteelyardServerModule mSteelyardServerModule =mModuleManager.loadModule(SteelyardServerModule.class);
        SteelyardStatusManagerModule mSteelyardStatusModule=mModuleManager.loadModule(SteelyardStatusManagerModule.class);

        SteelyardDataModule mSteelyardDataModule=mModuleManager.loadModule(SteelyardDataModule.class);
        return new com.lkm.market2_0.m_imple.app_main.AppMainModuleImple(mSteelyardServerModule,mSteelyardStatusModule,mSteelyardDataModule);
    }

    @Override
    public Class getModuleInterfaceClass() {
        return AppMainModule.class;
    }

    @Override
    public void onError(ErrorWarp e) {

    }
}
